<template>
  <div class="wrapper">
    <!-- header部分 -->
    <header>
      <p>地址管理</p>
    </header>
<!-- 返回按钮 -->
    <div class="back-button" @click="goBack">
      <i class="fa fa-arrow-left"></i>
      <!-- 使用 Font Awesome 图标 -->
    </div>
    <!-- 地址列表部分 -->
    <ul class="addresslist">
      <li v-for="item in addressList" :key="item.addressId">
        <div class="addresslist-left" @click="setDefaultAddress(item)">
          <h3>
            {{item.contactName }}
            {{item.contactSex==0?'先生':'女士'}}
            {{item.contactPhone }}
          </h3>
          <p>{{item.address}}</p>
        </div>
        <div class="addresslist-right">
          <i class="fa fa-edit" @click="editUserAddress(item)"></i>
          <i class="fa fa-remove" @click="removeUserAddress(item)"></i>
        </div>
      </li>
    </ul>

    <!-- 新增地址部分 -->
    <div class="addbtn" @click="toAddUserAddress">
      <i class="fa fa-plus-circle"></i>
      <p>新增收货地址</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      user: JSON.parse(sessionStorage.getItem("user")), //当前登录的用户信息
      addressList: [], //收货地址列表
      businessId: this.$route.query.businessId
    };
  },
  mounted() {
    this.getAddressList();
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    //选择默认收货地址
    setDefaultAddress(address) {
      // console.log(address)
      //将默认收货地址存放到sessionStroage中
      sessionStorage.setItem("defaultAddress", JSON.stringify(address));
      this.$router.push({
        path: "/order",
        query: { businessId: this.businessId }
      });
    },
    //API-查询当前用户的收货地址
    getAddressList() {
      this.$http
        .get("/address/listAddressByUser", {
          params: {
            userId: this.user.userId
          }
        })
        .then(res => {
          //console.log(res)
          this.addressList = res.data.object;
        });
    },
    //点击新增收货地址
    toAddUserAddress() {
      this.$router.push({
        path: "/addUserAddress",
        query: { businessId: this.businessId }
      });
    },
    //点击编辑收货地址
    editUserAddress(address) {
      this.$router.push('/editUserAddress'); // 简单跳转测试
      this.$router.push({
        path: "/editUserAddress",
        query: {
          businessId: this.businessId,
          address: JSON.stringify(address)
        }
      });
    },
    //点击删除收货地址
    removeUserAddress(address) {
      //console.log(address)
      this.$http
        .post("/address/deleteAddress", address)
        .then(res => {
          //console.log(res.data);
          // this.$router.push({path:"/userAddress",query:{businessId:this.businessId}});
          this.getAddressList();
        });
    }
  }
};
</script>

<style scoped>
/*************** 总容器 ***************/
.wrapper {
  width: 100%;
  height: 100%;
  background-color: #f5f5f5;
}

/*************** header ***************/
.wrapper header {
  width: 100%;
  height: 12vw;
  background-color: #0097ff;
  display: flex;
  justify-content: space-around;
  align-items: center;
  color: #fff;
  font-size: 4.8vw;
  position: fixed;
  left: 0;
  top: 0;
  /*保证在最上层*/
  z-index: 1000;
}

/*************** addresslist ***************/
.wrapper .addresslist {
  width: 100%;
  margin-top: 12vw;
  background-color: #fff;
}

.wrapper .addresslist li {
  width: 100%;
  box-sizing: border-box;
  border-bottom: solid 1px #ddd;
  padding: 3vw;

  display: flex;
}

.wrapper .addresslist li .addresslist-left {
  flex: 5;
  /*左边这块区域是可以点击的*/
  user-select: none;
  cursor: pointer;
}

.wrapper .addresslist li .addresslist-left h3 {
  font-size: 4.6vw;
  font-weight: 300;
  color: #666;
}

.wrapper .addresslist li .addresslist-left p {
  font-size: 4vw;
  color: #666;
}

.wrapper .addresslist li .addresslist-right {
  flex: 1;
  font-size: 5.6vw;
  color: #999;
  cursor: pointer;

  display: flex;
  justify-content: space-around;
  align-items: center;
}

/*************** 新增地址部分 ***************/
.wrapper .addbtn {
  width: 100%;
  height: 14vw;
  border-top: solid 1px #ddd;
  border-bottom: solid 1px #ddd;
  background-color: #fff;
  margin-top: 4vw;

  display: flex;
  justify-content: center;
  align-items: center;

  font-size: 4.5vw;
  color: #0097ff;
  user-select: none;
  cursor: pointer;
}

.wrapper .addbtn p {
  margin-left: 2vw;
}
.back-button {
  position: fixed;
  top: 5vw; /* 根据需要调整高度 */
  left: 4vw; /* 给左边留出一定间距 */
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #ffffff;
  font-size: 4vw;
  z-index: 1000;
}
</style>